package com.qimou.iems.controller;

import com.qimou.iems.entity.AdminUser;
import com.qimou.iems.entity.PageArgs;
import com.qimou.iems.entity.PageList;
import com.qimou.iems.entity.User;
import com.qimou.iems.exception.InterfaceCommonException;
import com.qimou.iems.service.IAdminUserService;
import com.qimou.iems.util.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Qimou Xie
 * @date 2018/4/14 16:20.
 **/

@RestController
@RequestMapping(value = "/adminUser")
public class AdminUserController extends BaseController {

    @Resource
    private IAdminUserService adminUserService;


    @RequestMapping(value = "/getAdminUserList")
    public ViewDataPage getAdminUserList(PageArgs pageArgs){
        Map<String,Object> map = new HashMap<String, Object>();
        PageList<AdminUser> adminUserList = adminUserService.getAdminUserList(map, pageArgs);
        return buildSuccessViewDataPage(StatusConstant.SUCCESS_CODE,"获取成功",
                adminUserList.getTotalSize(),adminUserList.getList());
    }


    @RequestMapping(value = "updateIsValid",method = RequestMethod.POST)
    public ViewData del(Integer id,Integer isValid){
        if(CommonUtil.isEmpty(id,isValid)){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数不能为空");
        }
        AdminUser adminUser = new AdminUser();
        adminUser.setId(id);
        adminUser.setIsValid(isValid);
        adminUserService.update(adminUser);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ViewData save(String account,String pwd,String roleIds,String realName){

        if(CommonUtil.isEmpty(account,pwd,roleIds)){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数不能为空");
        }

        AdminUser adminUser = adminUserService.getAdminUser(account);
        if(null != adminUser){
            return buildFailureJson(StatusConstant.OBJECT_EXIST,"帐号已经存在");
        }
        AdminUser add = new AdminUser();
        add.setAccount(account);
        add.setPwd(pwd);
        add.setRealName(realName);
        adminUserService.save(add,roleIds);
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ViewData login(String account, String pwd, HttpServletRequest request){

        if(CommonUtil.isEmpty(account,pwd)){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"用户名密码错误");
        }
        try {
            AdminUser adminUser = adminUserService.login(account, pwd);
            request.getSession().setAttribute(LoginHelper.LOGIN_SESSION,adminUser);
            return buildSuccessJson(StatusConstant.SUCCESS_CODE,"登录成功",adminUser);
        } catch (InterfaceCommonException e) {
            return buildFailureJson(e.getErrorCode(),e.getMessage());
        }
    }

    /**
     * 退出登录
     */
    @RequestMapping(value = "/logout",method = RequestMethod.POST)
    public ViewData logout(HttpServletRequest request){
        request.getSession().invalidate();
        return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"操作成功");
    }


    @RequestMapping(value = "/editPassword",method = RequestMethod.POST)
    public ViewData editPassword(String newPwd,String oldPwd){
        if(CommonUtil.isEmpty(newPwd,oldPwd)){
            return buildFailureJson(StatusConstant.FIELD_NOT_NULL,"参数不能为空");
        }
        try {
            AdminUser user = LoginHelper.getCurrentUser();
            if(!oldPwd.equals(user.getPwd())){
                return buildFailureJson(StatusConstant.Fail_CODE,"旧密码不正确");
            }
            AdminUser u = new AdminUser();
            u.setId(user.getId());
            u.setPwd(newPwd);
            adminUserService.update(u);
            return buildSuccessCodeJson(StatusConstant.SUCCESS_CODE,"更新成功");
        } catch (InterfaceCommonException e) {
            return buildFailureJson(e.getErrorCode(),e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return buildFailureJson(StatusConstant.Fail_CODE,"操作失败");
        }
    }



}
